package zabbix

import (
	"encoding/json"
	"errors"
)

func HostgroupCreateData() (string,map[string]interface{}) {
	method := "hostgroup.create"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "name": "Linux servers"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostgroupDeleteData() (string,map[string]interface{}) {
	method := "hostgroup.delete"
	var data map[string]interface{}
	jsonstr := []byte(`[
		"107824",
		"107825"
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostgroupMassremoveData() (string,map[string]interface{}) {
	method := "hostgroup.massremove"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "groupids": [
            "5",
            "6"
        ],
        "hostids": [
            "30050",
            "30001"
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostgroupMassupdateData() (string,map[string]interface{}) {
	method := "hostgroup.massupdate"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "groups": [
            {
                "groupid": "6"
            }
        ],
        "hosts": [
            {
                "hostid": "30050"
            }
        ]
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostgroupMassaddData() (string,map[string]interface{}) {
	method := "hostgroup.massadd"
	var data map[string]interface{}
	jsonstr := []byte(`[
        "groups": [
            {
                "groupid": "5"
            },
            {
                "groupid": "6"
            }
        ],
        "hosts": [
            {
                "hostid": "30050"
            },
            {
                "hostid": "30001"
            }
        ]
	]`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostgroupUpdateData() (string,map[string]interface{}) {
	method := "hostgroup.update"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "groupid": "7",
        "name": "Linux hosts"
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}

func HostgroupGetData() (string,map[string]interface{}) {
	method := "hostgroup.get"
	var data map[string]interface{}
	jsonstr := []byte(`{
        "output": "extend",
        "filter": {
            "name": [
                "Zabbix servers",
                "Linux servers"
            ]
        }
	}`)
	json.Unmarshal(jsonstr,&data)
	return method,data
}


func HostgroupCreate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupCreateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostgroupDelete(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupDeleteData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostgroupMassremove(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupMassremoveData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostgroupMassupdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupMassupdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostgroupMassadd(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupMassaddData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostgroupUpdate(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupUpdateData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}

func HostgroupGet(url string,user string,password string) (interface{},*API) {
	api := LoginAndHandle(url,user,password)
	method,data := HostgroupGetData()
	result,err:=api.ZabbixRequest(method,data)
	if err != nil {
		return nil,api
	}
	if result.Error.Code != 0 {
		err = errors.New(result.Error.Message)
		return nil,api
	}
	resultdata := result.Result
	return resultdata,api
}


